package com.opencee.boot.autoconfigure.swagger.autoconfigure;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Swagger文档生成配置
 *
 * @author liuyadu
 */
@Slf4j
@Configuration
@EnableSwagger2WebMvc
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {

    private SwaggerProperties properties;
    private Environment environment;

    public SwaggerAutoConfiguration(SwaggerProperties properties, Environment environment) {
        this.properties = properties;
        this.environment = environment;
    }

    /**
     * 获取接口文档信息
     *
     * @return
     */
    public ApiInfo getApiInfo() {
        String env = environment.getProperty("spring.profiles.active");
        List<VendorExtension> extensions = new ArrayList<>();
        // 当前环境
        extensions.add(new StringVendorExtension("env", env));
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title(properties.getTitle())
                .description(properties.getDescription())
                .termsOfServiceUrl(properties.getTermsOfServiceUrl())
                .version(properties.getVersion())
                .extensions(extensions)
                .build();
        return apiInfo;
    }

    /**
     * swagger配置类
     */
    @Configuration
    public class Swagger2Configuration {

        @Bean
        public Docket createRestApi() {
            ApiInfo apiInfo = getApiInfo();
            log.info("开启Swagger文档:{}", JSONObject.toJSONString(apiInfo));
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo)
                    .select()
                    .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)
                            .or(RequestHandlerSelectors.withMethodAnnotation(Controller.class)))
                    .build()
                    // 添加全局参数
                    //.globalOperationParameters(parameters())
                    .securitySchemes(Collections.singletonList(securityScheme()))
                    .enable(properties.isEnable());
        }

        /**
         * 构建全局参数
         *
         * @return
         */
        private List<Parameter> parameters() {
            ParameterBuilder builder = new ParameterBuilder();
            List<Parameter> pars = new ArrayList<Parameter>();
            builder = new ParameterBuilder();
            pars.add(builder.build());
            return pars;
        }


        @Bean
        UiConfiguration uiConfig() {
            return UiConfigurationBuilder.builder()
                    .docExpansion(DocExpansion.LIST)
                    .operationsSorter(OperationsSorter.ALPHA)
                    .defaultModelRendering(ModelRendering.MODEL)
                    .supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
                    .build();
        }


        /***
         * oauth2配置
         * 需要增加swagger授权回调地址
         * http://localhost:8888/webjars/springfox-swagger-ui/o2c.html
         * @return
         */
        @Bean
        SecurityScheme securityScheme() {
            return new ApiKey("BearerToken", "Authorization", "header");
        }

    }


}
